package com.zsj.gulimall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsj.gulimall.common.utils.PageUtils;

import com.zsj.gulimall.product.dao.CategoryDao;
import com.zsj.gulimall.product.entity.CategoryEntity;
import com.zsj.gulimall.product.service.CategoryService;
import com.zsj.gulimall.common.utils.Query;


@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        //开始组装树形父子结构
        //1 找到所有的1级结果
        return categoryEntities.stream().filter((categoryEntity) -> {
            return categoryEntity.getParentCid() == 0;//没有父亲就是孤儿
        }).map((menu) -> {
            menu.setChildren(getChildren(menu, categoryEntities));
            return menu;
        }).sorted((m1, m2) -> {
            return m1.getSort() - m2.getSort();
        }).collect(Collectors.toList());
    }

    //递归寻找所有子菜单
    private List<CategoryEntity> getChildren(CategoryEntity cur, List<CategoryEntity> all) {
        return all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid().equals(cur.getCatId());
        }).map(categoryEntity -> {
            //递归再次查找
            categoryEntity.setChildren(getChildren(categoryEntity, all));
            return categoryEntity;
        }).sorted((m, m2) -> {
            return (m.getSort() == null ? 0 : m.getSort()) - (m2.getSort() == null ? 0 : m2.getSort());
        }).collect(Collectors.toList());
    }

    @Override
    public void removeMenuByIds(List<Long> list) {
        //校验 TODO 检查当前删除的菜单是否被别的地方引用
        // 比如要删除的是汽车菜单 但是汽车菜单是保时捷的父节点  这种不给他删 或者全删了
         /*    List<CategoryEntity> all = baseMapper.selectList(null);
       int size = list.size();  无限递归查询是否有子节点
        for (int i = 0; i < list.size(); i++) {
            QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("cat_id", list.get(i));
            //如果查询到当前节点是一个父节点 需要把父节点内所有的节点全部递归删除
            CategoryEntity one = baseMapper.selectOne(queryWrapper);
            List<CategoryEntity> children = getAllChildrenOrNull(one, all);
            if (!children.isEmpty()) {
                for (CategoryEntity child : children) {
                    if (!list.contains(child.getCatId())) list.add(child.getCatId());
                }
            }
        }*/
        baseMapper.deleteBatchIds(list);//直接根据id批量删除
    }



}